package org.stvd.listener.workflow;

import java.util.HashSet;
import java.util.Set;

import org.activiti.engine.delegate.event.ActivitiEntityEvent;
import org.activiti.engine.delegate.event.ActivitiEvent;
import org.activiti.engine.impl.persistence.entity.TaskEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.stvd.common.workflow.WorkflowConsts;
import org.stvd.common.workflow.enums.WorkflowApproveStatusEnum;
import org.stvd.core.util.DateUtil;
import org.stvd.core.util.StringUtil;
import org.stvd.entities.admin.Users;
import org.stvd.entities.workflow.WfIndex;
import org.stvd.entities.workflow.WfLogs;
import org.stvd.entities.workflow.WfLogsAffix;
import org.stvd.service.admin.UsersService;
import org.stvd.service.workflow.WfIndexService;
import org.stvd.service.workflow.WfLogsAffixService;
import org.stvd.service.workflow.WfLogsService;

import com.alibaba.druid.util.StringUtils;

/**
 * 任务被完成了。它会在 ENTITY_DELETE事件之前触发。当任务是流程一部分时，事件会在流程继续运行之前， 后续事件将是
 * ACTIVITY_COMPLETE，对应着完成任务的节点。
 */
public class TaskCompletedListener implements EventHandler {

	@Autowired
	WfIndexService wfIndexService;

	@Autowired
	WfLogsService wfLogsService;

	@Autowired
	WfLogsAffixService wfLogsAffixService;
	@Autowired
	private UsersService usersService;

	public void handle(ActivitiEvent event) {

		System.out.println("Event received: " + event.getType());

		ActivitiEntityEvent entityEvent = (ActivitiEntityEvent) event;
		TaskEntity taskEntity = (TaskEntity) entityEvent.getEntity();

		WfIndex wfIndex = wfIndexService.findByProcInstId(taskEntity.getProcessInstanceId());
		if (wfIndex != null) {
			String histUsers = wfIndex.getHistUsers();
			if (!StringUtil.isEmpty(histUsers)) {
				histUsers += "," + wfIndex.getAssignee();
			} else {
				histUsers = wfIndex.getAssignee();
			}
			wfIndex.setStatus(WorkflowApproveStatusEnum.Processed.getValue());
			wfIndex.setHistUsers(histUsers);
			wfIndexService.update(wfIndex);
		}
		WfLogs wfLogs = new WfLogs();

		wfLogs.setOperator(taskEntity.getVariable(WorkflowConsts.INDEX_USER_ID).toString());
		if (!StringUtils.isEmpty(wfLogs.getOperator())) {
			Users user = usersService.findById(wfLogs.getOperator());
			if (user != null) {
				wfLogs.setOperatorName(user.getUname());
			} else {
				wfLogs.setOperatorName(taskEntity.getVariable(WorkflowConsts.INDEX_USER_NAME).toString());
			}
		}

		wfLogs.setOperateContent(taskEntity.getVariable(WorkflowConsts.INDEX_SUGGESTION).toString());
		wfLogs.setOperateTime(DateUtil.getSystemDate());
		wfLogs.setOperateType(taskEntity.getVariable(WorkflowConsts.INDEX_OPERATE_TYPE).toString().split(":")[0]);
		wfLogs.setOperateName(taskEntity.getVariable(WorkflowConsts.INDEX_OPERATE_TYPE).toString().split(":")[1]);
		wfLogs.setProcInstId(taskEntity.getProcessInstanceId());
		wfLogs.setTaskId(taskEntity.getId());
		wfLogs.setTaskName(taskEntity.getName());
		wfLogs.setRemark("");
		wfLogsService.insert(wfLogs);

		Object affixIdObject = taskEntity.getVariable("affix_id");
		if (affixIdObject != null) {
			String[] affixIds = {};
			if (affixIdObject instanceof String) {
				Set<String> idSet = new HashSet<String>();
				idSet.add(affixIdObject.toString());
				affixIds = idSet.toArray(affixIds);
			} else if (affixIdObject instanceof String[]) {
				affixIds = (String[]) affixIdObject;
			}
			for (String affixId : affixIds) {
				WfLogsAffix wfLogsAffix = new WfLogsAffix();
				wfLogsAffix.setLogId(wfLogs.getId());
				wfLogsAffix.setProcInstId(wfLogs.getProcInstId());
				wfLogsAffix.setAffixId(affixId);
				wfLogsAffix.setAffixFilename(taskEntity.getVariable("affix_filename_" + affixId).toString());
				wfLogsAffix.setAffixExtension(taskEntity.getVariable("affix_extension_" + affixId).toString());
				wfLogsAffix.setAffixLocalUrl(taskEntity.getVariable("affix_local_url_" + affixId).toString());
				wfLogsAffix.setAffixRemoteUrl(taskEntity.getVariable("affix_remote_url_" + affixId).toString());
				wfLogsAffix.setAffixSize(taskEntity.getVariable("affix_size_" + affixId).toString());
				wfLogsAffixService.insert(wfLogsAffix);
			}
		}
	}

}